﻿using System;
using UnityEngine;

namespace RootMotion.FinalIK
{
	// Token: 0x02000141 RID: 321
	[Serializable]
	public class Finger
	{
		// Token: 0x17000032 RID: 50
		// (get) Token: 0x060004D4 RID: 1236 RVA: 0x00015345 File Offset: 0x00013745
		// (set) Token: 0x060004D5 RID: 1237 RVA: 0x0001534D File Offset: 0x0001374D
		public bool initiated { get; private set; }

		// Token: 0x17000033 RID: 51
		// (get) Token: 0x060004D6 RID: 1238 RVA: 0x00015356 File Offset: 0x00013756
		// (set) Token: 0x060004D7 RID: 1239 RVA: 0x00015363 File Offset: 0x00013763
		public Vector3 IKPosition
		{
			get
			{
				return this.solver.IKPosition;
			}
			set
			{
				this.solver.IKPosition = value;
			}
		}

		// Token: 0x17000034 RID: 52
		// (get) Token: 0x060004D8 RID: 1240 RVA: 0x00015371 File Offset: 0x00013771
		// (set) Token: 0x060004D9 RID: 1241 RVA: 0x0001537E File Offset: 0x0001377E
		public Quaternion IKRotation
		{
			get
			{
				return this.solver.IKRotation;
			}
			set
			{
				this.solver.IKRotation = value;
			}
		}

		// Token: 0x060004DA RID: 1242 RVA: 0x0001538C File Offset: 0x0001378C
		public bool IsValid(ref string errorMessage)
		{
			if (this.bone1 == null || this.bone2 == null || this.tip == null)
			{
				errorMessage = "One of the bones in the Finger Rig is null, can not initiate solvers.";
				return false;
			}
			return true;
		}

		// Token: 0x060004DB RID: 1243 RVA: 0x000153CC File Offset: 0x000137CC
		public void Initiate(Transform hand, int index)
		{
			this.initiated = false;
			string empty = string.Empty;
			if (!this.IsValid(ref empty))
			{
				Warning.Log(empty, hand, false);
				return;
			}
			this.solver = new IKSolverLimb();
			this.solver.IKPositionWeight = this.weight;
			this.solver.bendModifier = IKSolverLimb.BendModifier.Target;
			this.solver.bendModifierWeight = 1f;
			this.IKPosition = this.tip.position;
			this.IKRotation = this.tip.rotation;
			if (this.bone3 != null)
			{
				this.bone3RelativeToTarget = Quaternion.Inverse(this.IKRotation) * this.bone3.rotation;
				this.bone3DefaultLocalPosition = this.bone3.localPosition;
				this.bone3DefaultLocalRotation = this.bone3.localRotation;
			}
			this.solver.SetChain(this.bone1, this.bone2, this.tip, hand);
			this.solver.Initiate(hand);
			this.initiated = true;
		}

		// Token: 0x060004DC RID: 1244 RVA: 0x000154DC File Offset: 0x000138DC
		public void FixTransforms()
		{
			if (!this.initiated)
			{
				return;
			}
			this.solver.FixTransforms();
			if (this.bone3 != null)
			{
				this.bone3.localPosition = this.bone3DefaultLocalPosition;
				this.bone3.localRotation = this.bone3DefaultLocalRotation;
			}
		}

		// Token: 0x060004DD RID: 1245 RVA: 0x00015534 File Offset: 0x00013934
		public void Update(float masterWeight)
		{
			if (!this.initiated)
			{
				return;
			}
			float num = this.weight * masterWeight;
			if (num <= 0f)
			{
				return;
			}
			this.solver.target = this.target;
			if (this.target != null)
			{
				this.IKPosition = this.target.position;
				this.IKRotation = this.target.rotation;
			}
			if (this.bone3 != null)
			{
				if (num >= 1f)
				{
					this.bone3.rotation = this.IKRotation * this.bone3RelativeToTarget;
				}
				else
				{
					this.bone3.rotation = Quaternion.Lerp(this.bone3.rotation, this.IKRotation * this.bone3RelativeToTarget, num);
				}
			}
			this.solver.IKPositionWeight = num;
			this.solver.Update();
		}

		// Token: 0x04000219 RID: 537
		[Tooltip("Master Weight for the finger.")]
		[Range(0f, 1f)]
		public float weight = 1f;

		// Token: 0x0400021A RID: 538
		[Tooltip("The first bone of the finger.")]
		public Transform bone1;

		// Token: 0x0400021B RID: 539
		[Tooltip("The second bone of the finger.")]
		public Transform bone2;

		// Token: 0x0400021C RID: 540
		[Tooltip("The (optional) third bone of the finger. This can be ignored for thumbs.")]
		public Transform bone3;

		// Token: 0x0400021D RID: 541
		[Tooltip("The fingertip object. If your character doesn't have tip bones, you can create an empty GameObject and parent it to the last bone in the finger. Place it to the tip of the finger.")]
		public Transform tip;

		// Token: 0x0400021E RID: 542
		[Tooltip("The IK target (optional, can use IKPosition and IKRotation directly).")]
		public Transform target;

		// Token: 0x04000220 RID: 544
		private IKSolverLimb solver;

		// Token: 0x04000221 RID: 545
		private Quaternion bone3RelativeToTarget;

		// Token: 0x04000222 RID: 546
		private Vector3 bone3DefaultLocalPosition;

		// Token: 0x04000223 RID: 547
		private Quaternion bone3DefaultLocalRotation;
	}
}
